package ua.eug.airv1.orders;

import ua.eug.airv1.ImageProvider;
import ua.eug.airv1.controllers.ScreenController;
import ua.eug.airv1.map.TilePos;
import ua.eug.airv1.utils.Config;
import ua.eug.airv1.utils.EDirection;
import ua.eug.airv1.weapon.IWeapon;
import ua.eug.airv1.weapon.ZWeapon;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class WeaponOrder extends Actor {
	
	public static WeaponOrder ZOrder = new WeaponOrder(ZWeapon.Nobody);
	
	private Boolean active = false;
	private Boolean orderDone = false;
	private IWeapon owner;
	private TilePos start = new TilePos();
	private EOrder[] seq = new EOrder[]
			{ EOrder.NONE, EOrder.NONE, EOrder.NONE, EOrder.NONE, EOrder.NONE };	// last position always NONE
	
	public WeaponOrder(IWeapon owner) {
		super();
		this.owner = owner;
		
		if(!owner.equals(ZWeapon.Nobody)) {			
			OrderHolder.getInstance().addOrder(this);
		} else {
			orderDone = true;
		}
	}

	public void setStep ( int step, EOrder o) {
		if (step<seq.length)
			seq[step] = o;
	}
	public EOrder getStep (int step) {
		if (step>=seq.length) return EOrder.NONE ;
		return seq[step];
	}
	
	public void setActive ( ) {
		OrderHolder.getInstance().setActive(this);
		active = true;
	}
	public void resetActive ( ) {
		active = false;
	}
	public Boolean getOrderDone() {
		return orderDone;
	}
	public void setOrderDone(Boolean orderDone) {
		this.orderDone = orderDone;
	}
	
	@Override
    public void draw(Batch batch, float alpha){
		if (active) {
			start.set(owner.getTile().pos);
			EDirection dir = owner.getDirection();
			for (EOrder e : seq) {
				if (e == EOrder.NONE) return;
				Vector2 p = ScreenController.getInstance().XyToStage(start.x, start.y);
				dir = dir.getNext(e);
				batch.draw(ImageProvider.Arrow, Config.ArrowShiftX + p.x, Config.ArrowShiftY + p.y,
						Config.ArrowOriginX, Config.ArrowOriginY,
						Config.ArrowSizeX, Config.ArrowSizeY, 1,1, dir.getAngle());
				start.nextHex(dir);
			}
		}
    }

}
